package com.useeinfo.oa.open.dashboard.security;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.action.BaseAction;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import com.useeinfo.framework.sugar.tools.ResultJson;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.security.biz.SystemAuthorityBiz;
import com.useeinfo.oa.modules.security.biz.SystemModuleBiz;
import com.useeinfo.oa.modules.security.entity.SystemAuthority;
import com.useeinfo.oa.modules.security.entity.SystemRole;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * Author: 居泽平
 */
@Controller
@RequestMapping("/web/admin/systemAuthority")
public class SystemAuthorityAction extends BaseAction {

    @Autowired
    private SystemAuthorityBiz systemAuthorityBiz;
    @Autowired
    private SystemModuleBiz systemModuleBiz;

    // ******************************************************************************
    // ******************************* 代码自动生成开始 *******************************
    // ******************************************************************************

    /**
     * 打开列表页面
     */
    @RequestMapping("/getSystemAuthorityListPage")
    public ModelAndView getSystemAuthorityListPage() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/system/systemAuthority/systemAuthorityList");

        return modelAndView;
    }

    /**
     * 分页获取JSON数据
     */
    @RequestMapping("/getSystemAuthorityListJSON")
    @ResponseBody
    public JSONObject getSystemAuthorityListJSON(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                                 @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                                 @RequestParam(value = "orderBy", required = false) String orderBy,
                                                 @RequestParam(value = "searchCode", required = false) String searchCode) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("searchCode", searchCode);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        return systemAuthorityBiz.findJSONList(queryParam);
    }

    /**
     * 获取详情页面
     */
    @RequestMapping("/getSystemAuthorityViewPage")
    public ModelAndView getSystemAuthorityViewPage(@RequestParam(value = "authorityId", required = false) String authorityIdParam) {

        Long authorityId = StringConverters.ToLong(authorityIdParam);

        SystemAuthority systemAuthority = null;
        if (authorityId != null) {
            systemAuthority = systemAuthorityBiz.findModel(authorityId);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/system/systemAuthority/systemAuthorityViewPart");
        modelAndView.addObject("systemAuthority", systemAuthority);
        return modelAndView;
    }

    /**
     * 获取编辑页面
     */
    @RequestMapping("/getSystemAuthorityEditPage")
    public ModelAndView getSystemAuthorityEditPage(@RequestParam(value = "authorityId", required = false) String authorityIdParam) {

        Long authorityId = StringConverters.ToLong(authorityIdParam);

        SystemAuthority systemAuthority = null;
        if (authorityId != null) {
            systemAuthority = systemAuthorityBiz.findModel(authorityId);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/system/systemAuthority/systemAuthorityEditPart");
        modelAndView.addObject("systemAuthority", systemAuthority);
        return modelAndView;
    }

    /**
     * 执行提交的新增或修改请求
     */
    @RequestMapping("/executeSystemAuthorityEdit")
    @ResponseBody
    public ResultDto executeSystemAuthorityEdit(SystemAuthority systemAuthority,
                                                @RequestParam(value = "select_module[]", required = false) List<String> modules) {
        if (CollectionUtils.isNotEmpty(modules))
            systemAuthority.setSystemModule(systemModuleBiz.findModel(StringConverters.ToLong(modules.get(0))));
        //判断权限名称是否重复
        if (systemAuthorityBiz.existAuthorityName(systemAuthority)) {
            return ResultUtil.error("您输入的权限名称为空或已存在，请重新输入");
        }
        systemAuthorityBiz.addOrUpdate(systemAuthority);
        return ResultUtil.success();
    }

    /**
     * 逻辑删除机构用户信息
     */
    @RequestMapping("/logicRemoveSystemAuthority")
    @ResponseBody
    public ResultDto logicRemoveSystemAuthority(@RequestParam(value = "authorityId", required = false) String authorityIdParam,
                                                @RequestParam(value = "isFakeDelete", required = false) String isFakeDelete) {

        Long authorityId = StringConverters.ToLong(authorityIdParam);

        Set<SystemRole> systemRoleSet = this.systemAuthorityBiz.findModel(authorityId).getRoleSet();
        if (CollectionUtils.isEmpty(systemRoleSet)) {
            systemAuthorityBiz.delete(authorityId);
            return ResultUtil.success();
        }

        return ResultUtil.error("已关联数据,无法删除!");
    }

    /**
     * 获取所有权限数据
     */
    @RequestMapping("/getRoleAuthoritys")
    @ResponseBody
    public ResultDto getRoleAuthoritys(@RequestParam(value = "sysCode", required = false) String sysCode,
                                       @RequestParam(value = "roleId", required = false) String roleId,
                                       HttpServletRequest request) {
        Enterprise enterprise = CommonSugar.getTypedDefault(SessionKeyUtils.getSessionEnterprise(request), new Enterprise());
        return systemAuthorityBiz.getRoleAuthoritys(sysCode, roleId, enterprise.getId());
    }

    // ******************************************************************************
    // ******************************* 代码自动生成结束 *******************************
    // ******************************************************************************
}